package com.lixin.smartclassroom.utils

import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNet
import com.lixin.smartclassroom.api.uploadFiles
import com.lixin.smartclassroom.bean.ImageUploadEvent
import com.luck.picture.lib.entity.LocalMedia
import java.io.File
import java.nio.file.Files

class UploadUtils {
    companion object {
        fun uploadImages(list: ArrayList<LocalMedia>, position: Int, tags: String) {
            val files = ArrayList<File>()
            scopeNet {
                for (i in 0 until list.size) {
                    val file = when (list[i].isCompressed) {
                        true -> File(list[i].compressPath)
                        else -> File(list[i].realPath)
                    }
                    files.add(file)
                }
                scopeNet {
                    val baseBean = uploadFiles(files)
                    if (baseBean.result == "0") {
                        var urlString = ""
                        for (i in 0 until baseBean.urls?.size!!) {
                            urlString = when (urlString.isEmpty()) {
                                true -> baseBean.urls!![i]
                                else -> urlString + "|" + baseBean.urls!![i]
                            }
                        }
                        val imageUploadEvent = ImageUploadEvent(position, urlString)
                        sendEvent(imageUploadEvent, tags)
                    }
                }
            }
        }
    }

}